package cn.iocoder.yudao.module.station.service.cylinder;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.station.controller.admin.cylinder.vo.*;
import cn.iocoder.yudao.module.station.dal.dataobject.cylinder.CylinderDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.station.convert.cylinder.CylinderConvert;
import cn.iocoder.yudao.module.station.dal.mysql.cylinder.CylinderMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.station.enums.ErrorCodeConstants.*;

/**
 * 钢瓶档案 Service 实现类
 *
 * @author 林子
 */
@Service
@Validated
public class CylinderServiceImpl implements CylinderService {

    @Resource
    private CylinderMapper cylinderMapper;

    @Override
    public Long createCylinder(CylinderCreateReqVO createReqVO) {
        // 插入
        CylinderDO cylinder = CylinderConvert.INSTANCE.convert(createReqVO);
        cylinderMapper.insert(cylinder);
        // 返回
        return cylinder.getId();
    }

    @Override
    public void updateCylinder(CylinderUpdateReqVO updateReqVO) {
        // 校验存在
        validateCylinderExists(updateReqVO.getId());
        // 更新
        CylinderDO updateObj = CylinderConvert.INSTANCE.convert(updateReqVO);
        cylinderMapper.updateById(updateObj);
    }

    @Override
    public void deleteCylinder(Long id) {
        // 校验存在
        validateCylinderExists(id);
        // 删除
        cylinderMapper.deleteById(id);
    }

    private void validateCylinderExists(Long id) {
        if (cylinderMapper.selectById(id) == null) {
            throw exception(CYLINDER_NOT_EXISTS);
        }
    }

    @Override
    public CylinderDO getCylinder(Long id) {
        return cylinderMapper.selectById(id);
    }

    @Override
    public List<CylinderDO> getCylinderList(Collection<Long> ids) {
        return cylinderMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<CylinderDO> getCylinderPage(CylinderPageReqVO pageReqVO) {
        return cylinderMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CylinderDO> getCylinderList(CylinderExportReqVO exportReqVO) {
        return cylinderMapper.selectList(exportReqVO);
    }

}
